using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;

namespace WPF_DtaGrid_MVVM模式
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class MainViewModel : ViewModelBase
    {
        localDb localDb;
        string _search = string.Empty;

        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel()
        {
            localDb = new localDb();
            QueryCommand = new RelayCommand(Query);
            //重置查询
            ResetCommand = new RelayCommand(() =>
            {
                Search = string.Empty;
                this.Query();
            });
            //编辑栏
            EditCommand = new RelayCommand<int>(t => Edit(t));
            //删除栏
            DelCommand = new RelayCommand<int>(t => Del(t));


        }
        
        private ObservableCollection<Student> _gridModelList;

        public ObservableCollection<Student> GridModelList
        {
            get { return _gridModelList; }
            set 
            {
                _gridModelList = value;
                RaisePropertyChanged();
            }
        }

        #region Command
        /// <summary>
        /// 查询全部命令
        /// </summary>
        public RelayCommand QueryCommand { get; set; }
        /// <summary>
        /// 刷新命令
        /// </summary>
        public RelayCommand ResetCommand { get; set; }
        /// <summary>
        /// 编辑命令
        /// </summary>
        public RelayCommand<int> EditCommand { get; set; }
        /// <summary>
        /// 删除命令
        /// </summary>
        public RelayCommand<int> DelCommand { get; set; }
        
        #endregion

        public string Search 
        {
            get { return _search;}
            set
            {
                _search = value;
                RaisePropertyChanged();
            }
        }

        //提供事件绑定的方法
        public void Edit(int id)
        {
            var model = localDb.GetStudentById(id);
            if (model != null)
            {
                UserView view = new UserView(model);
                var r = view.ShowDialog();
                if (r.Value)
                {
                   var newModel =  GridModelList.FirstOrDefault(t => t.Id == model.Id);
                    if (newModel != null)
                    {
                        newModel.Name = model.Name;
                    }

                }
            }
        }

        //删除方法
        public void Del(int id)
        {
            var model = localDb.GetStudentById(id);
            if (model != null)
            {
                var r = MessageBox.Show(string.Format("确认删除当前用户{0}", model.Name), "操作提示", MessageBoxButton.OK, MessageBoxImage.Question);
                if (r==MessageBoxResult.OK)
                {
                    localDb.DelStudent(model.Id);
                }
                this.Query();//更新一下数据
            }
        }

        /// <summary>
        /// 将对象的值赋值给绑定的属性
        /// </summary>
        public void Query()
        {
            //获取全部的内容
            var models = localDb.GetStudentsByName(Search);
            //构建绑定的数据
            GridModelList = new ObservableCollection<Student>();
            if (models!=null)
            {
                models.ForEach(arg =>
                {
                    GridModelList.Add(arg);
                });

            }
        }


    }
}